package com.example.rbac.service.impl;

import com.example.rbac.entity.Menu;
import com.example.rbac.entity.Role;
import com.example.rbac.entity.RoleMenu;
import com.example.rbac.mapper.RoleMapper;
import com.example.rbac.service.MenuService;
import com.example.rbac.service.RoleService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/10/16.
 */
@Service
public class RoleServiceImpl extends CurdServiceImpl<RoleMapper, Role> implements RoleService{


    @Autowired
    private MenuService menuService;

    @Override
    @Async("taskExecutor")
    public List<Role> findList() {
        System.out.println("线程名称："+Thread.currentThread().getName() + " be ready to read data!");
//        try {
//            Thread.sleep(1000 * 5);
//            System.out.println("---------------------》》》无返回值延迟3秒：");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return repository.findList();
    }

    @Override
    @Transactional
    public Integer insertRole(Role role) {
        //添加角色对应的权限，并设置未开启
        List<Menu> menuList = menuService.findList();
        List<String> menuIds = Lists.newLinkedList();
        for (Menu menu : menuList){
            menuIds.add(menu.getId());
        }
        String id = UUID.randomUUID().toString().replace("-", "");
        role.setId(id);
        role.setState("1");
        role.setUpdateTime(new Date());
        role.setCreateTime(new Date());
        insertByPermissions(role.getId(), menuIds);
        return repository.insertRole(role);
    }

    @Override
    public Integer insertByPermissions(String roleId, List<String> roleMenuList) {
        return repository.insertByPermissions(roleId, roleMenuList);
    }

    @Transactional
    public void test1(){
        System.out.println("test1");
    }

    public void test2(){
        System.out.println(this);
        Class<? extends RoleServiceImpl> aClass = getClass();
        Method[] methods = aClass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i]);
        }
        test1();
        System.out.println("test2");
    }
}
